﻿using System;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;

namespace ImpExpData.Core
{
    public abstract class ExportData
    {
        public string FileName { get; set; }
        public string TableName { get; set; }
        protected SqlCeResultSet rs; 

        protected virtual void Export()
        {
            XmlWriter xml = null;
            try
            {
                xml = XmlWriter.Create(FileName);
                DataRowCollection cols = rs.GetSchemaTable().Rows;
                xml.WriteStartDocument();
                while (rs.Read())
                {
                    xml.WriteStartElement(TableName);
                    for (int i = 0; i < rs.FieldCount; i++)
                    {
                        xml.WriteStartAttribute(cols[i]["ColumnName"].ToString());
                        xml.WriteValue(rs.GetString(i));
                        xml.WriteEndAttribute();
                    }
                    xml.WriteEndElement();
                }
                xml.WriteEndDocument();
            }
                catch(Exception ex)
                {
                    throw ex;
                }
            finally
            {
                if (xml != null)
                    xml.Close();
            }
        }
    }
}
